package lar.system.network;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import lar.system.engine.MainProgram;
import lar.system.opengl.GLColor;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

/**
 * This class represents an abstract in-game connection between you and another player.
 * It also holds pre-game information about the other party such as name, id and color.
 * @author Roma
 *
 */
public abstract class Connection 
{
	private Integer id;
	private String name;
	private GLColor color;
	
	public Connection()
	{
		name = null;
		id = null;
		color = null;
	}
	

	public void setColor(GLColor c)
	{
		this.color = c;
	}
	
	public GLColor getColor()
	{
		return this.color;
	}
	
	public String getName()
	{
		return name;
	}
	
	public void setName(String name)
	{
		this.name = name;
	}
	
	public void setId(int id)
	{
		this.id = Integer.valueOf(id);
	}
	
	public int getId()
	{
		return this.id.intValue();
	}
	
	
	
	public abstract void send(String msg);
	
	public static Inet4Address getLocalIpAddress() 
	{
	    try 
	    {
	        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) 
	        {
	            NetworkInterface intf = en.nextElement();
	            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) 
	            {
	                InetAddress inetAddress = enumIpAddr.nextElement();
	                if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) 
	                    return (Inet4Address) inetAddress;
	            }
	        }
	    } catch (SocketException ex) {}
	    return null;
	}
	
}
